package com.jd.platform.sample2.config;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.RedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
///**
// * @author wuweifeng wrote on 2017/10/27.
// */
//@Configuration
//public class RedisConfig {
////    @Bean
//    @Bean(name = {"redisTemplate", "stringRedisTemplate"})
//    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
////        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        RedisTemplate redisTemplate = new RedisTemplate();
//
//        redisTemplate.setConnectionFactory(factory); //设置redis的连接工厂对象
//        redisTemplate.setKeySerializer(new StringRedisSerializer()); //设置redis key的序列化器
//        redisTemplate.setValueSerializer(new StringRedisSerializer()); //设置redis value的序列化器
////        redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //设置redis hash value序列化
////        redisTemplate.setHashValueSerializer(redisSerializer()); //设置redis hash value序列化
////        redisTemplate.afterPropertiesSet(); //初始化时的逻辑
//        return redisTemplate;
//    }
//
//    public RedisSerializer<Object> redisSerializer() {
//        //创建JSON序列化器
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        //必须设置，否则无法序列化实体类对象
//        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
//        return new GenericJackson2JsonRedisSerializer(objectMapper);
//    }
//}


import com.jd.platform.sample2.entity.properties.Redis2Properties;
import com.jd.platform.sample2.entity.properties.RedisProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

@Configuration
public class RedisConfig {
    @Resource
    private RedisProperties redisProperties;

    @Bean(name = "redisconnectionFactory")
    public RedisConnectionFactory redisconnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(redisProperties.getHost());
        config.setPort(redisProperties.getPort());
        config.setPassword(RedisPassword.of(redisProperties.getPassword()));
        config.setDatabase(redisProperties.getDatabase());
        return new JedisConnectionFactory(config);
    }

    @Bean(name = {"redisTemplate", "stringRedisTemplate"})
    public StringRedisTemplate stringRedisTemplate() {
        StringRedisTemplate redisTemplate = new StringRedisTemplate();

        redisTemplate.setConnectionFactory(redisconnectionFactory()); //设置redis的连接工厂对象
        redisTemplate.setKeySerializer(new StringRedisSerializer()); //设置redis key的序列化器
//        redisTemplate.setValueSerializer(new StringRedisSerializer()); //设置redis value的序列化器
//        redisTemplate.afterPropertiesSet(); //初始化时的逻辑
        return redisTemplate;
    }

}
